package com.lmy.androidcustomview.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.os.Build
import android.util.AttributeSet
import android.view.View
import androidx.annotation.RequiresApi
import androidx.core.content.res.ResourcesCompat
import com.lmy.androidcustomview.R
import com.lmy.androidcustomview.dp

/**
 * @author
 * @功能: 圆环进度条
 * @Creat 2/9/21 9:48 AM
 * @Compony 465008238@qq.com
 */
class SportView(context: Context, attributeSet: AttributeSet) : View(context, attributeSet) {
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        //设置字体大小
        textSize = 100.dp
        //设置字体风格
        typeface = ResourcesCompat.getFont(context,
            R.font.font
        )
        //设置居中显示
        textAlign = Paint.Align.CENTER
    }
    private val RADIUS = 150.dp
    private val fontMetrics = Paint.FontMetrics()

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        //绘制外层空心圆 设置画笔为瞄边模式
        paint.style = Paint.Style.STROKE
        paint.color = Color.parseColor("#7bdc97")
        paint.strokeWidth = 20.dp
        canvas.drawCircle(width / 2f, height / 2f, RADIUS, paint)
        // 线条的帽子 也就是线的两端设置圆的
        paint.strokeCap = Paint.Cap.ROUND
        paint.color = Color.parseColor("#86c1f2")
        paint.strokeWidth = 20.dp
        canvas.drawArc(
            width / 2f - RADIUS,
            height / 2f - RADIUS,
            width / 2f + RADIUS,
            height / 2f + RADIUS,
            -90f,
            230f,
            false,
            paint
        )
        val bounds = Rect()
        //把画笔模式修改为填充
        paint.style = Paint.Style.FILL
        val name = "abab"
        //测量文字的信息 得到上下左右 的偏移量
        paint.getTextBounds(name, 0, name.length, bounds)
//        paint.getFontMetrics(fontMetrics)
        //然后减去 顶部和底部之和/2 得到绘制点的纵向偏移量
        canvas.drawText(name, width / 2f, height / 2f - (bounds.top + bounds.bottom) / 2f, paint)

        //绘制文字2
        paint.textAlign = Paint.Align.LEFT
        paint.getTextBounds(name, 0, name.length,bounds)
        canvas.drawText(name, 0f-bounds.left, 0f - bounds.top, paint)

        //绘制文字3
        paint.textAlign = Paint.Align.LEFT
        paint.textSize = 15.dp
        paint.getTextBounds(name, 0, name.length,bounds)
        canvas.drawText(name, 0f-bounds.left, 0f - bounds.top, paint)

    }
}